<?php 
	/**
	 * 提现提交
	 */
    session_start();
    require_once("../includes/public.inc.php");
    $openid = isset($_SESSION['openid'])?$_SESSION['openid']:'';
    $tx_amount = $_REQUEST['amount'];
    $openusers =  mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where openid='".$openid."' order by id desc limit 1"));
	//print_r($openusers);exit;
    if (!empty($openid)) 
    {
    	$openusers =  mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where openid='".$openid."' order by id desc limit 1"));
    	if ($openusers)
    	{
    		if ($openusers['credit']<=0)
    		{
    			echo "<script>alert(亲，你还需要继续努力哦~你的红包余额不足');history.back();</script>";
       			exit;
    		}
    		if ($tx_amount<0)
    		{
    			echo "<script>alert('提现金额不能为负数！');history.back();</script>";
       			exit;
    		}
    		if ($tx_amount<2||$tx_amount>200)
    		{
    			echo "<script>alert('提现金额不能小于2元，不能大于200元！');history.back();</script>";
       			exit;
    		} 		
    		if ($openusers['credit']<$tx_amount)
    		{
    			echo "<script>alert('该用户禁止提现！');history.back();</script>";
       			exit;
    		}
    		else 
    		{
    			$sql="insert into ".DBQIAN."tixian (amount,time,status,notes,openid,buyer_id) values(".$tx_amount.",'".date('Y-m-d H:i:s',time())."',0,'提现','".$openusers['openid']."',".$openusers['id'].")";
				$create = mysql_query($sql);
				$create_id = mysql_insert_id();
				if ($create_id)
				{
					$credit=$openusers['credit']-$tx_amount;
					$updatesql= "update ".DBQIAN."users SET credit=".$credit." where openid ='".$openid."'";
					$create_query = mysql_query($updatesql);
					if ($create_query)
					{
						//转到发红包页面，或者转款页面，修改提现状态，提现时间
						header("Location: ".WEBNAME."main/chaikai.index.php?sk=1445313033&sh=43920&type=user&openid=".$openid."&id=".$create_id);exit;
					}
					else 
					{
						$deletesql= "delete from ".DBQIAN."tixian where id =".$create_id;
						$delete_query = mysql_query($deletesql);
						echo "<script>alert('提现失败，请检查数据的正确性！');history.back();</script>";
       					exit;
					}
				}
				else 
				{
					echo "<script>alert('提现失败，请检查数据的正确性！');history.back();</script>";
       				exit;
				}
    		}
    	}
    	else 
    	{
    		echo "<script>alert('该用户禁止提现！');history.back();</script>";
       		exit;
    	}
        $signlog = mysql_query("select * from ".DBQIAN."qiandao where openid='".$openid."'");  
    }
    else
    {
       echo "<script>alert('非法进入！');history.back();</script>";
       exit;
    }
?>